package com.jxtti.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.jxtti.entity.ArticleList;
import com.jxtti.service.ArticleListService;
import com.jxtti.utils.BaseCodec;

/**
 * 内网门户
 * @author bff
 *
 */

@Controller
@RequestMapping("/WebIntranetPc") 
public class WebIntranetPcController {
	
	private final static Logger log= Logger.getLogger(WebSiteIndexController.class);
	
	@InitBinder  //初始化数据的时候，进行数据类型转换，String类型转为Date类型
    public void initBinder(WebDataBinder binder) {     
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");     
        dateFormat.setLenient(false);     
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); 
    }
	
	//首页index.htm
	@RequestMapping(value = "/indexInterface")
	@ResponseBody
	public ModelAndView indexInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {									
			mav.addObject("data",data);
			mav.setViewName("/pages/IntranetPc/index.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	@Autowired 
	private ArticleListService  articleListService;
	
	//查找文章--内网文章
	@RequestMapping("/queryIntranetArticle")
	@ResponseBody
	public List<ArticleList> queryIntranetArticle(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{	
		data.put("is_deleted", "0");
		data.put("status", "2");//状态 草稿0,待审1,审核通过2,审核失败3
		data.put("articleSite", "'1','2'");//文章所属站点 0 外网,1内网，2内外网-----内网文章		
		data.put("issue", "1");//是否网页显示
		// data.put("isHomePic", "1");//是否首页图片新闻		
		List<ArticleList>  articleList= articleListService.queryListForIndex(data);
		return articleList;
	}
	
	//文章 article.htm
	@RequestMapping(value = "/IntranetArticle")
	@ResponseBody
	public ModelAndView IntranetArticleInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			String artid=new String(BaseCodec.decode(data.get("id").toString()));//解密			
			ArticleList art=new ArticleList();
			art.setId(Integer.parseInt(artid));			
			ArticleList  article= articleListService.queryById(art);//查找文章
			data.put("article", article);
			
			if(data.get("preview")==null){ //preview 预览						
				article.setClicks(article.getClicks()+1);
				articleListService.update(article);
			}			
						
			//文章的上一篇 下一篇
			Map<String, Object> preData= new HashMap<String, Object>();
			preData.put("is_deleted", "0");
			preData.put("status", "2");//状态 草稿0,待审1,审核通过2,审核失败3
			preData.put("articleSite", "'1','2'");//文章所属站点 0 外网,1内网，2内外网		
			preData.put("issue", "1");//是否网页显示
			preData.put("midd", artid);//中间 id
			preData.put("categoryID", article.getCategoryID());
			List<ArticleList> preArticle= articleListService.queryPreNext(preData);//查找文章
			data.put("preArticle", preArticle);	
			
			mav.addObject("data",data);
			mav.setViewName("/pages/IntranetPc/IntranetArticleContent.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	//列表页面
	@RequestMapping(value = "/articleListInterface")
	@ResponseBody
	public ModelAndView articleListInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {			
			String catid=new String(BaseCodec.decode(data.get("id").toString()));//解密	
			data.put("categoryId", catid);
			mav.addObject("data",data);
			mav.setViewName("/pages/IntranetPc/ArticleList.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
		
	//查找文章--分页
	@RequestMapping("/queryArticlePage")
	@ResponseBody
	public Map<String, Object> queryArticlePag(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{	
		data.put("is_deleted", "0");
		data.put("status", "2");//状态 草稿0,待审1,审核通过2,审核失败3
		data.put("articleSite", "'1','2'");//文章所属站点 0 外网,1内网，2内外网		
		data.put("issue", "1");//是否网页显示
		
		Map<String, Object> result = new HashMap<String, Object>();		
		PageInfo<ArticleList> page = articleListService.listPage(data);
		result.put("total", page.getTotal());
		result.put("rows", page.getList());				
		return result;
	}
	
	
	//列表页面
	@RequestMapping(value = "/oaList")
	@ResponseBody
	public ModelAndView oaList(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {		
			
			mav.setViewName("http://10.162.121.51/ycyd/enterprise/detailOutSide.wag?enterId=11466");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	@RequestMapping(value = "/oaList2")
	@ResponseBody
	public ModelAndView oaList2(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {			
			
			response.sendRedirect("http://10.162.121.51/ycyd/enterprise/detailOutSide.wag?enterId=11466");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	
	
	
	

}
